package com.ruoyi.member.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aliyun.green20220302.Client;
import com.ruoyi.api.feign.ContentModerationService;
import com.ruoyi.common.context.AppUserContext;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.RedisKeyEnum;
import com.ruoyi.common.enums.cms.CmsErrorCode;
import com.ruoyi.common.exception.modules.CmsException;
import com.ruoyi.member.entity.domain.MobileUser;
import com.ruoyi.member.entity.req.MobileUserInfoReq;
import com.ruoyi.member.entity.resp.MobileUserInfoResp;
import com.ruoyi.member.mapper.MobileUserMapper;
import com.ruoyi.member.service.MobileUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 移动端用户Service业务层处理
 *
 * @date 2025-05-29
 */
@Service
@Slf4j
public class MobileUserServiceImpl implements MobileUserService {

    @Autowired
    private MobileUserMapper mobileUserMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ContentModerationService contentModerationService;

    @Override
    public R<?> edit(MobileUserInfoReq req) {
        check(req);

        MobileUser mobileUser = mobileUserMapper.selectById(AppUserContext.getUser().getUserId());
        mobileUser.setName(req.getName());
        mobileUser.setAvatar(req.getAvatar());
        mobileUser.setProfile(req.getProfile());
        mobileUser.setPhoneNumber(req.getPhoneNumber());
        mobileUserMapper.updateById(mobileUser);

        redisCache.deleteObject(RedisKeyEnum.USER_INFO.getPrefix() + AppUserContext.getUser().getUserId());
        getInfo(AppUserContext.getUser().getUserId());

        return R.ok();
    }

    private void check(MobileUserInfoReq req) {
        Client client;
        try {
            client = contentModerationService.createSZClient();
        } catch (Exception e) {
            log.error("edit user moderation error", e);
            throw new CmsException("edit user moderation error");
        }

        //审核昵称
        if (!contentModerationService.textModeration(req.getName(), client)) {
            throw new CmsException(CmsErrorCode.TEXT_VIOLATION.getCode(), CmsErrorCode.TEXT_VIOLATION.getMsg());
        }
        //审核简介
        if (!contentModerationService.textModeration(req.getProfile(), client)) {
            throw new CmsException(CmsErrorCode.TEXT_VIOLATION.getCode(), CmsErrorCode.TEXT_VIOLATION.getMsg());
        }
        //审核头像
        if (!contentModerationService.imageUrlModeration(req.getAvatar(), client)) {
            //删除oss文件
            throw new CmsException(CmsErrorCode.IMAGE_VIOLATION.getCode(), CmsErrorCode.IMAGE_VIOLATION.getMsg());
        }
    }

    @Override
    public R<MobileUserInfoResp> getInfo(Long userId) {
        String key = RedisKeyEnum.USER_INFO.getPrefix() + userId;

        if (redisCache.hasKey(key)) {
            MobileUserInfoResp mobileUserInfoResp = redisCache.getCacheObject(key);
            return R.ok(mobileUserInfoResp);
        }

        MobileUser mobileUser = mobileUserMapper.selectById(userId);

        MobileUserInfoResp resp = new MobileUserInfoResp();
        resp.setId(mobileUser.getId());
        resp.setAvatar(mobileUser.getAvatar());
        resp.setProfile(mobileUser.getProfile());
        resp.setName(mobileUser.getName());

        redisCache.setCacheObject(key, resp, RedisKeyEnum.USER_INFO.getTimeout(), RedisKeyEnum.USER_INFO.getTimeUnit());

        return R.ok(resp);
    }

}
